<?php
	class Image {
		private $image;
		private $width;
		private $height;
		private $generateImage;
		private $generateImageQuality = 90;
		
		public static function GDInfo() {
			return gd_info();
		}
		
		/**
		 *创建原始图片
		 *@param $params 已存在图片路径/生成图片的尺寸数组array(width,height)
		*/
		public function __construct($params = null) {
			if(is_null($params)) {
				throw new Exception('请输入参数！',500);
			}
			$params = func_get_arg(0);
			if(is_array($params)) {
				$width = abs(intval($params[0]));
				$height = abs(intval($params[1]));
				if($width == 0 || $height == 0) {
					throw new Exception('参数输入有误！',500);
				}
				$this->image = imagecreatetruecolor($width,$height);
				$this->width = $width;
				$this->height = $height;
			}else {
				if(file_exists($params) === false) {
					throw new Exception('图片文件不存在！',500);
				}
				$imageParams = getimagesize($params);
				$this->width = $imageParams[0];
				$this->height = $imageParams[1];
				$this->image = imagecreatefromstring(file_get_contents($params));
			}
			if(is_resource($this->image) === false) {
				throw new Exception('GD图片初始化失败',500);
			}
		}
		
		/**
		 *改变图片尺寸
		 *@param $width 缩略图宽度
		 *@param $height 缩略图高度
		 *@param $proportion 是否等比例缩放，默认为false
		*/
		public function resize($width,$height,$proportion = false) {
			if($proportion) {
				if($width == 0 && $height == 0) {
					throw new Exception('生成缩略图的宽高不能同时设置为0',500);
				}
				if ($width) {
					$height = ($width / $this->width) * $this->height;
				} else {
					$width = ($height / $this->height) * $this->width;
				}
			}else {
				if($width == 0 || $height == 0) {
					throw new Exception('生成缩略图的宽高不能设置为0',500);
				}
			}
			
			$this->generateImage = imagecreatetruecolor($width,$height);
			
			//处理透明度
			$temp = imagecolortransparent($this->image);
			if($temp >= 0 && $temp < imagecolorstotal($this->image)){
				$temp = imagecolorsforindex($this->image, $temp);
				$temp = imagecolorallocate($this->generateImage,$temp['red'],$temp['green'],$temp['blue']);
				imagefill($this->generateImage,0,0,$temp);
				imagecolortransparent($this->generateImage,$temp);
			}else {
				imagealphablending($this->generateImage, true); 
				$temp = imagecolorallocatealpha( $this->generateImage, 0, 0, 0, 127 ); 
				imagefill( $this->generateImage, 0, 0, $temp ); 
			}
			
			imagecopyresampled($this->generateImage,$this->image,0,0,0,0,$width,$height,$this->width,$this->height);
			imagealphablending($this->generateImage, false);
		}
		
		/**
		 *剪切图片
		 *@param $_cx 剪切起点坐标 x
		 *@param $_cy 剪切起点坐标 y
		 *@param $_cw 剪切宽度
		 *@param $_ch 剪切高度
		*/
		public function cut($_cx,$_cy,$_cw,$_ch) {
			$this->generateImage = imagecreatetruecolor($_cw,$_ch);
			
			imagecopyresampled($this->generateImage,$this->image,0,0,$_cx,$_cy,$_cw,$_ch,$_cw,$_ch);
		}
		
		/**
		 *显示或保存图片
		 *@param $imageType 图片格式
		 *
		*/
		private function processImage($imageType = 'jpeg',$params = null) {
			switch($imageType) {
				case 'jpg' : 
				case 'jpeg': $func = 'jpeg';break;
				case 'png' : $func = 'png';break;
				case 'gif' : $func = 'gif';break;
				default: throw new Exception('不支持该图片格式！格式：' . $imageType,500);
			}
			if($func == 'png') {
				imagesavealpha($this->generateImage,true);
			}
			if(is_null($params)) {
				header("Content-type: image/{$func}");
				call_user_func('image' . $func,$this->generateImage);
			}else {
				if(!file_exists(dirname($params[0]))) {
					throw new Exception('文件路径不存在！' . $params[0],500);
				}
				$funcParams[] = $this->generateImage;
				$funcParams[] = $params[0];
				
				if($func == 'jpeg') {
					if(!empty($params[1])) {
						$funcParams[] = $params[1];
					}else {
						$funcParams[] = $this->generateImageQuality;
					}
				}
				
				call_user_func_array('image' . $func,$funcParams);
				
				return basename($params[0]);
			}
		}
		
		public function __call($func,$args) {
			if(substr($func,0,4) === 'save'){
				$imageType = substr($func,4);
				return $this->processImage(strtolower($imageType),$args);
			}elseif(substr($func,0,4) === 'show') {
				$imageType = substr($func,4);
				return $this->processImage(strtolower($imageType));
			}
		}
		
		public function & getImageResource() {
			return $this->image;
		}
		
		public function & getGenerateImageResource() {
			return $this->generateImage;
		}
		
		public function __destruct() {
			if(is_resource($this->image)) {
				imagedestroy($this->image);
			}
			if(is_resource($this->generateImage)) {
				imagedestroy($this->generateImage);
			}
		}
	}